package com.tjp.eight.ui.fragment.bindingphone;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ResultModel;
import com.tjp.eight.RxBus;
import com.tjp.eight.constant.ConstantUtils;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.Cutils;
import com.tjp.eight.utils.DescriptUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.utils.TimeDown;
import com.tjp.eight.utils.ToatUtils;

import org.hamcrest.core.Is;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/3/23.
 */
public class FragmentBindingPhone extends BaseFragment<BindingPhonePresenter> implements BindingPhoneView {
    @Bind(R.id.et_binding_phone)
    EditText et_binding_phone;
    @Bind(R.id.ed_phone_code)
    EditText ed_phone_code;
    @Bind(R.id.tv_get_code)
    TextView tv_get_code;
    @Bind(R.id.tv_binding_submit)
    TextView tv_binding_submit;

    private String phone_num = "";
    private TimeDown timeDown;

    @Override
    protected BindingPhonePresenter createPresenter() {
        return new BindingPhonePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_binding_phone, container, false);
        }
        ButterKnife.bind(this, contentView);
        initViews();
        return contentView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (timeDown != null) {
            timeDown.cancel();
        }
    }

    private void initViews() {
        et_binding_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String phone = s.toString();
                phone_num = phone;
                if (StringUtils.isMobileNO(phone)) {
                    tv_get_code.setEnabled(true);
                } else {
                    tv_get_code.setEnabled(false);
                }
                if (StringUtils.isMobileNO(phone) && ed_phone_code.getText().toString().length() == 6) {
                    tv_binding_submit.setEnabled(true);
                } else {
                    tv_binding_submit.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ed_phone_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtils.isMobileNO(et_binding_phone.getText().toString()) && ed_phone_code.getText().toString().length() == 6) {
                    tv_binding_submit.setEnabled(true);
                } else {
                    tv_binding_submit.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick(R.id.tv_get_code)
    void onClickCode() {
        if (!StringUtils.isMobileNO(phone_num)) {
            ToatUtils.toastSuccess(getContext(), "请输入正确的手机号码");
            return;
        }
        timeDown();
        presenter.bindingphonecode(getCodePJson());
    }

    @OnClick(R.id.tv_binding_submit)
    void submit() {
        presenter.submit(getBindingPhonePJson());
    }

    private void timeDown() {
        tv_get_code.setEnabled(false);
        if (timeDown == null)
            timeDown = new TimeDown(60, new TimeDown.TimeDownListener() {
                @Override
                public void onPrepareCountDown() {

                }

                @Override
                public void onRefreshTime(int second) {
                    tv_get_code.setText(second + "秒");
                }

                @Override
                public void onFinishCountDown() {
                    tv_get_code.setText("重新获取");
                    tv_get_code.setEnabled(true);
                    timeDown = null;
                }
            });
        timeDown.start();
    }

    private String getCodePJson() {
        BindingCodeModel model = new BindingCodeModel();
        model.mobile = phone_num;
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
//        model.time = System.currentTimeMillis() / 1000 + "";
//        model.authcode = Cutils.getInstance().getCode();
//        model.md5authcode = DescriptUtils.SHA1(model.time + model.authcode + model.authcode + ConstantUtils.SHA1);
        return new Gson().toJson(model);
    }

    private String getBindingPhonePJson() {
        BindingPhonePModel model = new BindingPhonePModel();
        model.mobile = phone_num;
        model.mobilecode = ed_phone_code.getText().toString();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(model);
    }

    @Override
    public void getCodeSuccess(ResultModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result))
                ToatUtils.toastSuccess(getContext(), "短信验证码已发送");
            else
                ToatUtils.toastSuccess(getContext(), model.msg);
        }
    }

    @Override
    public void subSuccess(ResultModel model) {
        if (model != null) {
            ToatUtils.toastSuccess(getContext(), model.msg);
            if (IsSuccessUtils.isSuccess(model.result)){
                RxBus.getInstance().post(new BindingPhoneEvent(phone_num));
            }
        }
    }

}
